﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.ComponentModel;
using System.Threading;

namespace carXapp2
{
    public partial class MainPage : PhoneApplicationPage
    {
       

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            this.DataContext = App.ViewModel;
            
        }

        

       private void addClick(object sender, EventArgs e)
        {
            carInfo newCar = new carInfo();
            newCar.CarInsExpiry = DateTime.Now;
            newCar.CarRegExpiry = DateTime.Now;
            App.ViewModel.SingleCarRecord = newCar;
            NavigationService.Navigate(new Uri("/MiscPages/addEditCar.xaml",UriKind.Relative));
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            // Save changes to the database.
            App.ViewModel.SaveChangesToDB();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            listBox1.SelectedIndex = -1;
            NavigationService.RemoveBackEntry();
            NavigationService.RemoveBackEntry();
            base.OnNavigatedTo(e);
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                var selectCar = listBox1.SelectedItem as carInfo;
                App.ViewModel.SingleCarRecord =selectCar;
                string id = selectCar.CarID.ToString();
                NavigationService.Navigate(new Uri("/FuncPages/FuncMain.xaml?id="+id+"&index="+listBox1.SelectedIndex, UriKind.Relative));
            }
        }

        private void menuSettings_click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MiscPages/Settings.xaml", UriKind.RelativeOrAbsolute));
        }

        private void about_click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MiscPages/about.xaml", UriKind.RelativeOrAbsolute));
        }

        
    }
}